﻿using PGMHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.DataVisualization.Charting;

namespace WSAP
{
    /// <summary>
    /// 曲线集合
    /// </summary>
    public class WSAPCurveSet
    {
        /// <summary>
        /// 曲线列表
        /// </summary>
        private List<WSAPCurve> CuverList { set; get; }

        /// <summary>
        /// 绘图名称
        /// </summary>
        private List<string> titleNameList { set; get; }

        /// <summary>
        /// 添加曲线
        /// </summary>
        /// <param name="name"></param>
        /// <param name="xValues"></param>
        /// <param name="yValues"></param>
        public void Add(string name, List<double> xValues, List<double> yValues)
        {
            this.CuverList.Add(new WSAPCurve(name, xValues, yValues));
        }

        /// <summary>
        /// 添加曲线
        /// </summary>
        /// <param name="curveSet"></param>
        public void AddRange(WSAPCurveSet curveSet)
        {
            curveSet.CuverList.ForEach(curve => this.CuverList.Add(curve));
        }

        /// <summary>
        /// 绘图
        /// </summary>
        /// <param name="chart"></param>
        public void ToChart(Chart chart)
        {
            chart.AddTitles(this.titleNameList[0], this.titleNameList[1], this.titleNameList[2]);
            chart.Series.Clear();
            this.CuverList.ForEach(curve => curve.ToChart(chart));
        }

        /// <summary>
        /// 绘图
        /// </summary>
        public void ToChart()
        {
            var f = new WSAPCurveForm(this);
            f.Show();
        }

        /// <summary>
        /// 获得数据
        /// </summary>
        /// <returns></returns>
        public GridViewDatas GetData()
        {
            var data = new GridViewDatas();
            this.CuverList.ForEach(curve => data.AddRange(curve.GetData()));
            return data;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public WSAPCurveSet(string titleName, string xName, string yName)
        {
            this.CuverList = new List<WSAPCurve>();
            this.titleNameList = new List<string> { titleName, xName, yName };
        }
    }

    /// <summary>
    /// 曲线数据
    /// </summary>
    public class WSAPCurve
    {
        /// <summary>
        /// X坐标
        /// </summary>
        private List<double> xValueList { set; get; }

        /// <summary>
        /// Y坐标
        /// </summary>
        private List<double> yValueList { set; get; }

        /// <summary>
        /// 序列名称
        /// </summary>
        private string Name { set; get; }

        /// <summary>
        /// 绘图
        /// </summary>
        /// <param name="chart"></param>
        public void ToChart(Chart chart)
        {
            chart.AddSeries(this.Name, SeriesChartType.Line);
            chart.Series[this.Name].Points.DataBindXY(this.xValueList, this.yValueList);
        }
        
        /// <summary>
        /// 获得数据
        /// </summary>
        /// <returns></returns>
        public GridViewDatas GetData()
        {
            var data = new GridViewDatas();
            data.Add(new GridViewColumn(this.xValueList, titleName: this.Name + "X"));
            data.Add(new GridViewColumn(this.yValueList, titleName: this.Name + "Y"));
            return data;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="xValues"></param>
        /// <param name="yValues"></param>
        public WSAPCurve(string name, List<double> xValues, List<double> yValues)
        {
            this.Name = name;
            this.xValueList = xValues;
            this.yValueList = yValues;
        }
    }
}
